package vashion.azeroth.api.controller.member;

import java.util.ArrayList;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.api.controller.base.BaseController;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.contant.MemberErrorEnum;
import vashion.azeroth.core.manager.personal.PersonalFriendManager;
import vashion.azeroth.core.personal.pojo.PersonalFriend;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalConnectionService;
import vashion.azeroth.core.service.PersonalFriendService;

@Controller
@RequestMapping("/member")
public class FriendController<T> extends BaseController {

	private final static Logger log = LoggerFactory.getLogger(FriendController.class);
	
	@Autowired
	private PersonalConnectionService<T> connectionService;
	
	@Autowired
	private PersonalFriendService<T> friendService;
	
	@Autowired
	private PersonalFriendManager personalFriendManager;
	
	@SuppressWarnings("unchecked")
	@RequestMapping("getHigher.htm")
	@ResponseBody
	public ApiResult<PersonalMember> getHigher(
			@ModelAttribute("member") PersonalMember member) {

		ApiResult<PersonalMember> result = new ApiResult<PersonalMember>();
		try {
			if (null == member) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (null == member.getMemberId() || StringUtils.isBlank(String.valueOf(member.getMemberId()))) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "会员ID"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			result = (ApiResult<PersonalMember>) connectionService.getHigher(member);
		} catch (Exception e) {
			log.error("获取上线人脉失败", e);
			return ApiResult.newErrorResult(MemberErrorEnum.GET_FRIENDS_FAIL.getErrorCode(), MemberErrorEnum.GET_FRIENDS_FAIL.getErrorMessage());
		}
		return result;
	}
	
	@RequestMapping("getLowers.htm")
	@ResponseBody
	public ApiResult<ArrayList<PersonalMember>> getLowers(
			@ModelAttribute("member") PersonalMember member) {

		ApiResult<ArrayList<PersonalMember>> result = new ApiResult<ArrayList<PersonalMember>>();
		try {
			if (null == member) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (null == member.getMemberId() || StringUtils.isBlank(String.valueOf(member.getMemberId()))) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "会员ID"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}
			
			result = connectionService.getLowers(member);
		} catch (Exception e) {
			log.error("获取下线人脉失败", e);
			return ApiResult.newErrorResult(MemberErrorEnum.GET_FIRST_CONN_FAIL.getErrorCode(), MemberErrorEnum.GET_FIRST_CONN_FAIL.getErrorMessage());
		}
		return result;
	}
	
	@RequestMapping("getFirstLevelFriends.htm")
	@ResponseBody
	public ApiResult<ArrayList<PersonalFriend>> getFirstLevelFriends(
			@ModelAttribute("member") PersonalMember member) {

		ApiResult<ArrayList<PersonalFriend>> result = new ApiResult<ArrayList<PersonalFriend>>();
		try {
			if (null == member) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (null == member.getMemberId() || StringUtils.isBlank(String.valueOf(member.getMemberId()))) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "会员ID"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}
			result = friendService.getFirstLevelFriends(member);
		} catch (Exception e) {
			log.error("获取下线人脉失败", e);
			return ApiResult.newErrorResult(MemberErrorEnum.GET_FIRST_CONN_FAIL.getErrorCode(), MemberErrorEnum.GET_FIRST_CONN_FAIL.getErrorMessage());
		}
		return result;
	}
	
	@RequestMapping("getSecondLevelFriends.htm")
	@ResponseBody
	public ApiResult<ArrayList<PersonalFriend>> getSecondLevelFriends(
			@ModelAttribute("member") PersonalMember member) {

		ApiResult<ArrayList<PersonalFriend>> result = new ApiResult<ArrayList<PersonalFriend>>();
		try {
			if (null == member) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (null == member.getMemberId() || StringUtils.isBlank(String.valueOf(member.getMemberId()))) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "会员ID"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}
			result = friendService.getSecondLevelFriends(member);
		} catch (Exception e) {
			log.error("获取下线人脉失败", e);
			return ApiResult.newErrorResult(MemberErrorEnum.GET_FIRST_CONN_FAIL.getErrorCode(), MemberErrorEnum.GET_FIRST_CONN_FAIL.getErrorMessage());
		}
		return result;
	}
	
	@RequestMapping("saveFriend.htm")
	@ResponseBody
	public ApiResult<PersonalFriend> saveFriend(
			@ModelAttribute("member") PersonalMember member) {

		ApiResult<PersonalFriend> result = new ApiResult<PersonalFriend>();
		try {
			if (null == member) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (null == member.getMemberId() || StringUtils.isBlank(String.valueOf(member.getMemberId()))) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "会员ID"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}
			result = friendService.saveFriend(member);
		} catch (Exception e) {
			log.error("添加人脉失败", e);
			return ApiResult.newErrorResult(MemberErrorEnum.GET_FIRST_CONN_FAIL.getErrorCode(), MemberErrorEnum.GET_FIRST_CONN_FAIL.getErrorMessage());
		}
		return result;
	}
	
}
